package com.aounhui.productor.service.impl;

import com.aounhui.common.exceptions.SellCommodityException;
import com.aounhui.common.utils.UUIDUtil;
import com.aounhui.productor.common.SendMessage;
import com.aounhui.productor.entity.BaseProductor;
import com.aounhui.productor.entity.Commodity;
import com.aounhui.productor.entity.CommodityExample;
import com.aounhui.productor.entity.CommodityFactory;
import com.aounhui.productor.mapper.CommodityFactoryMapper;
import com.aounhui.productor.mapper.CommodityMapper;
import com.aounhui.productor.service.CommodityService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;

/**
 * @author aounhui
 * @version 1.0
 * @date 2021/6/15 10:21
 */
@Service
@Slf4j
public class CommodityServiceImpl implements CommodityService {

    @Autowired
    private CommodityMapper commodityMapper;
    @Autowired
    private CommodityFactoryMapper commodityFactoryMapper;
    @Autowired
    private SendMessage sendMessage;

    @Override
    public void commodityCreater(CommodityFactory commodityFactory) {
        Commodity commodity = Commodity.builder()
                .id(UUIDUtil.getUUID())
                .expireDate(commodityFactory.getExpireDate())
                .commodityName(commodityFactory.getName())
                .isSold(0)
                .isOverdue(0)
                .createTime(new Date()).build();
        commodityMapper.insertSelective(commodity);
    }

    @Override
    public List<Commodity> getCommodity(String commodityName, int number, String orderId) {
        CommodityExample example = new CommodityExample();
        CommodityExample.Criteria criteria = example.createCriteria();
        criteria.andCommodityNameEqualTo(commodityName);
        criteria.andIsOverdueEqualTo(0);
        criteria.andIsSoldEqualTo(0);
        example.setOrderByClause("create_time asc");
        Map<String, Object> param = new HashMap<>();
        param.put("commodityName", commodityName);
        param.put("isOverdue", 0);
        param.put("isSold", 0);
        param.put("order", "create_time asc");
        param.put("limit", number);
        List<Commodity> commodities = commodityMapper.selectCommodity(param);
        //取出货物，将库存的货物is_sold更新状态，sold_time更新时间
        List<String> consumCommodityIds = new ArrayList<>();
        commodities.stream().forEach((a)->consumCommodityIds.add(a.getId()));
        //库存货物取出，更新货物状态,发送生产此商品的消息
        sellCommodity(consumCommodityIds, orderId);
        log.info("{}产品有消费，发送生产消息", commodityName);
        sendMessage.sendMessage(commodityName, "please product" + commodityName);
        return commodities;
    }

    //将货物状态修改，售出
    private void sellCommodity(List<String> ids, String orderId) {
        ids.stream().forEach((a)->{
            Commodity commodity = commodityMapper.selectByPrimaryKey(a);
            //将要消费的货物售出状态和售出时间更新
            commodity.setIsSold(1);
            commodity.setSoldTime(new Date());
            commodity.setExts(orderId);
            commodityMapper.updateByPrimaryKeySelective(commodity);
        });
    }

    @Override
    public boolean satisfyCommodity(String commodityName) {
        CommodityFactory commodityFactory = commodityFactoryMapper.selectByPrimaryKey(commodityName);
        CommodityExample example = new CommodityExample();
        CommodityExample.Criteria criteria = example.createCriteria();
        criteria.andIsOverdueEqualTo(0);
        criteria.andIsSoldEqualTo(0);
        criteria.andCommodityNameEqualTo(commodityName);
        long count = commodityMapper.countByExample(example);
        boolean satisfy = count >= commodityFactory.getMaxNuber();
        log.info("查询{}当前存量为{}，{}满足最大库存{}", commodityName, count, satisfy, commodityFactory.getMaxNuber());
        return satisfy;
    }
}
